home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / source.exe / POSIX / CHOWN / CHOWGLOB.C < prev    next >
C/C++ Source or Header  |  1992-10-29  |  2KB  |  80 lines

  1. /*
  2.     xnglob.c
  3.  
  4.     The globulate function below takes an NT command line and expands
  5.     wildcards according to POSIX.2 rules. It returns 0 on success, or -1
  6.     on failure. The reason that *argv is always globbed on is to convert
  7.     any backslashes in the program name to slashes. The deglobulate
  8.     function below it merely frees the memory associated with the
  9.     previously run globulate.
  10. */
  11.  
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "glob.h"
  15.  
  16. static signed int globulated_argi;
  17. signed int globulated_argc;
  18. char **globulated_argv;
  19.  
  20. signed int globulate (signed int argi, signed int argc, char **argv)
  21. {
  22.     glob_t gl;
  23.     signed int ret, a;
  24.  
  25.     if (argi <= 0 || argc < argi || argv == NULL || (ret = glob(*argv, GLOB_NOCHECK, NULL, &gl)) != 0)
  26.         ret = -1;
  27.     else
  28.     {
  29.         for (a = argi; a < argc; ++a)
  30.         {
  31.             if ((ret = glob(argv[a], GLOB_NOCHECK|GLOB_APPEND|GLOB_SHQUOTE, NULL, &gl)) != 0)
  32.             {
  33.                 ret = -1;
  34.                 goto globfree_gl;
  35.             }
  36.         }
  37.         if ((globulated_argv = malloc((argi + gl.gl_pathc + 1) * sizeof *globulated_argv)) == NULL)
  38.         {
  39.             ret = -1;
  40.             goto globfree_gl;
  41.         }
  42.         if ((*globulated_argv = malloc(strlen(*(gl.gl_pathv)) + 1)) == NULL)
  43.         {
  44.             ret = -1;
  45.             goto globfree_gl;
  46.         }
  47.         (void) strcpy(*globulated_argv, *(gl.gl_pathv));
  48.         for (a = 1; a < argi; ++a)
  49.             globulated_argv[a] = argv[a];
  50.         globulated_argc = gl.gl_pathc - 1;
  51.         for (a = 0; a < globulated_argc; ++a)
  52.         {
  53.             if ((globulated_argv[a + argi] = malloc(strlen(gl.gl_pathv[a + 1]) + 1)) == NULL)
  54.             {
  55.                 while (a--)
  56.                     free(globulated_argv[a + argi]);
  57.                 free(*globulated_argv);
  58.                 ret = -1;
  59.                 goto globfree_gl;
  60.             }
  61.             (void) strcpy(globulated_argv[a + argi], gl.gl_pathv[a + 1]);
  62.         }
  63.         globulated_argv[a + argi] = NULL;
  64.         globulated_argc += argi;
  65.         globulated_argi = argi;
  66.         ret = 0;
  67. globfree_gl:
  68.         globfree(&gl);
  69.     }
  70.     return ret;
  71. }
  72.  
  73. void deglobulate (void)
  74. {
  75.     for (globulated_argi = globulated_argc - globulated_argi; globulated_argi--; )
  76.         free(globulated_argv[--globulated_argc]);
  77.     free(*globulated_argv);
  78.     free(globulated_argv);
  79. }
  80.